{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": [],
      "authorship_tag": "ABX9TyPuxIdo78z05I74eR2dUzJJ",
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/Huangjian2013/ai-demo/blob/main/rag/09-LangChain-Chains.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "N7rbJ7CbVUC8"
      },
      "outputs": [],
      "source": [
        "!pip install langchain --quiet\n",
        "!pip install langchain-openai --quiet"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "from google.colab import userdata\n",
        "from langchain_openai import ChatOpenAI\n",
        "from langchain_core.prompts import ChatPromptTemplate\n",
        "\n",
        "from langchain_core.output_parsers import StrOutputParser\n",
        "\n"
      ],
      "metadata": {
        "id": "pBXoK4AOXiZU"
      },
      "execution_count": 2,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# 使用的是Cloudflare的OpenAPI封装接口\n",
        "model = ChatOpenAI(model=\"@cf/meta/llama-2-7b-chat-int8\",\n",
        "          api_key = userdata.get('OPEN_AI_KEY'),\n",
        "          base_url = userdata.get('OPEN_AI_BASE_URL'))"
      ],
      "metadata": {
        "id": "xIQszadqXmUv"
      },
      "execution_count": 3,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "prompt_template = ChatPromptTemplate.from_messages([\n",
        "    (\"system\", \"将以下输入翻译成英文\"),\n",
        "    (\"human\", \"{input}\")\n",
        "])\n",
        "\n",
        "parser = StrOutputParser()\n",
        "\n",
        "chain = prompt_template | model | parser\n",
        "\n",
        "chain.invoke(\"我想买车了\")"
      ],
      "metadata": {
        "id": "BrHea_Iseacl",
        "outputId": "30c3b844-bed8-4eb2-84da-4b3d43bb90b8",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        }
      },
      "execution_count": 4,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'I want to buy a car'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 4
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "chain = prompt_template | model | parser\n",
        "\n",
        "back_to_Chinese_Prompt = ChatPromptTemplate.from_messages([\n",
        "    (\"system\", \"将以下输入翻译成中文。按以下格式输出：二次翻译 = <result>。比如输入'good'，输出'二次翻译 = 好'\"),\n",
        "    (\"human\", \"{input}\")\n",
        "])\n",
        "composed_chain = {\"input\": chain} | back_to_Chinese_Prompt | model | parser\n",
        "\n",
        "composed_chain.invoke(\"我想买车了\")"
      ],
      "metadata": {
        "id": "mK7JU1AVeexo",
        "outputId": "79db18b5-6e61-4105-f282-6d18dfe9348a",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        }
      },
      "execution_count": 5,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'二次翻译 = 我要购买一辆汽车。'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 5
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "composed_chain = chain | (lambda x: {\"input\": x}) | back_to_Chinese_Prompt | model | parser\n",
        "\n",
        "composed_chain.invoke(\"买什么车好\")\n"
      ],
      "metadata": {
        "id": "uidTAFH5eh8_",
        "outputId": "82f0a8a9-aa83-481f-f502-268c5c10c7a1",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        }
      },
      "execution_count": 6,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'二次翻译 = 这样的汽车购买有什么好选择？'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 6
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "from langchain_core.runnables import RunnableParallel\n",
        "\n",
        "chain_with_pipe = (RunnableParallel({\"input\": chain})\n",
        "                    .pipe(back_to_Chinese_Prompt)\n",
        "                    .pipe(model)\n",
        "                    .pipe(parser)\n",
        ")\n",
        "\n",
        "chain_with_pipe.invoke(\"买ROBO-01吧\")"
      ],
      "metadata": {
        "id": "r1n67UCFek2z",
        "outputId": "586eaf69-91a4-4206-e4bd-4bd53121da38",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        }
      },
      "execution_count": 8,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'二次翻译 = 购买ROBO-01！'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 8
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "first_chain = {\"input\": chain} | back_to_Chinese_Prompt | model | parser\n",
        "sec_chain = chain\n",
        "map_chain = RunnableParallel(chinese=first_chain, english=sec_chain)\n",
        "map_chain.invoke(\"买买买！快买吧!\")\n"
      ],
      "metadata": {
        "id": "JcHP3cP0e4_X",
        "outputId": "6307ec77-48c8-4fe2-cd3d-b227c3ad0a49",
        "colab": {
          "base_uri": "https://localhost:8080/"
        }
      },
      "execution_count": 9,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "{'chinese': '二次翻译 =.NAME好 buys!\".', 'english': 'Buy buy buy! Buy quickly!'}"
            ]
          },
          "metadata": {},
          "execution_count": 9
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "chain = prompt_template | model | parser\n",
        "\n",
        "async for chunk in chain.astream(\"异步买车的过程\"):\n",
        "    print(chunk, end=\"|\", flush=True)\n",
        "\n",
        "chain_with_pipe.invoke(\"买Robo-01吧\")"
      ],
      "metadata": {
        "id": "6jdcLi_re63-",
        "outputId": "6ddc78ff-e4b5-44b1-eb97-7b1250cebe16",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 87
        }
      },
      "execution_count": 11,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Here| is| the| translation| of| \"|异|步|买|车|的|过程|\"| to| English|:\n",
            "\n",
            "|\"As|ynchronous| Car| Buying| Process|\"|"
          ]
        },
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'二次翻译 = 让我们购买Robo-01。'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 11
        }
      ]
    }
  ]
}